<?php

namespace App\Http\Controllers\Admin;

use App\Admin\Message;
use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

class MessageController extends Controller
{
    //
    public function list(){
        $data = DB::table('message')
            ->join('manager','manager.id','=','message.user_id')
            ->join('topic','topic.id','=','message.top_id')
            ->where('message.status',0)
            ->select('message.*','manager.name','topic.title')
            ->paginate(10);
        return view('admin.message.list',compact('data'));
    }
    public function del(Request $request){
        $data =  $request->route( 'id' );

        $pid = DB::table('message')->where('id',$data)->value('pid');
        if ($pid == 0){
           $temp = DB::table('message')->where('pid',$data)->where('status',0)->get();
//dd($temp);
           if (count($temp)){
               foreach ($temp as $item) {
//                   dd($item->id);
                   Message::where('id',$item->id)->update(['status'=>1]);
               }
               return json_encode(['message'=>'删除成功']);
           }else{
               Message::where('id',$data)->update(['status'=>1]);
               return json_encode(['message'=>'删除成功']);
           }
        }
        Message::where('id',$data)->update(['status'=>1]);
        return json_encode(['message'=>'删除成功']);
    }
}
